﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using QuanLyKho.Common;

namespace QuanLyKho.DataAccess
{

    public  class clsDataAccessListEquipment
    {

        private SqlConnection conn = new clsConnect().GetSqlConnection();
        private SqlCommand cmd = null;
        private SqlDataReader dr = null;
        private StringBuilder sBuilder = null;

        #region iAction Members
        public int AddNew(object objParam)
        {
            try
            {
                int status = 0;
                conn.Open();
                clsEquipment obj = (clsEquipment)objParam;
                sBuilder = new StringBuilder("Sp_listEquipment_Add");
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Code", obj.Code);
                cmd.Parameters.AddWithValue("@Name", obj.Name);
                cmd.Parameters.AddWithValue("@Des", obj.Des);
                status = cmd.ExecuteNonQuery();
                conn.Close();
                return status;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

        public int Update(object objParam)
        {
            try
            {
                int status = 0;
                conn.Open();
                clsEquipment obj = (clsEquipment)objParam;
                sBuilder = new StringBuilder("Sp_listEquipment_Update");
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ID", obj.ID);
                cmd.Parameters.AddWithValue("@Code", obj.Code);
                cmd.Parameters.AddWithValue("@Name", obj.Name);
                cmd.Parameters.AddWithValue("@Des", obj.Des);
                status = cmd.ExecuteNonQuery();
                conn.Close();
                return status;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

        public int Delete(long id)
        {
            try
            {
                int status = 0;
                conn.Open();
                sBuilder = new StringBuilder("Sp_listEquipment_Delete");
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ID", id);
                status = cmd.ExecuteNonQuery();
                conn.Close();
                return status;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

        #endregion

        public List<clsEquipment> listEquipmentSelectAll()
        {
            try
            {
                List<clsEquipment> list = new List<clsEquipment>();
                sBuilder = new StringBuilder("Sp_listEquipment_SelectAll");
                conn.Open();
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        clsEquipment obj = new clsEquipment();
                        if (dr["ID"] != DBNull.Value)
                            obj.ID = Convert.ToInt64(dr["ID"]);
                        if (dr["Code"] != DBNull.Value)
                            obj.Code = Convert.ToString(dr["Code"]);
                        if (dr["Name"] != DBNull.Value)
                            obj.Name = Convert.ToString(dr["Name"]);
                        if (dr["Des"] != DBNull.Value)
                            obj.Des = Convert.ToString(dr["Des"]);
                        list.Add(obj);
                    }
                }
                conn.Close();
                return list;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }


        public List<clsEquipment> listEquipmentSelectOneWithId(long id)
        {
            try
            {
                List<clsEquipment> list = new List<clsEquipment>();
                sBuilder = new StringBuilder("Sp_listEquipment_SelectOne");
                conn.Open();
                cmd = new SqlCommand(sBuilder.ToString(), conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ID", id);
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        clsEquipment obj = new clsEquipment();
                        if (dr["ID"] != DBNull.Value)
                            obj.ID = Convert.ToInt64(dr["ID"]);
                        if (dr["Code"] != DBNull.Value)
                            obj.Code = Convert.ToString(dr["Code"]);
                        if (dr["Name"] != DBNull.Value)
                            obj.Name = Convert.ToString(dr["Name"]);
                        if (dr["Des"] != DBNull.Value)
                            obj.Des = Convert.ToString(dr["Des"]);
                        list.Add(obj);
                    }
                }
                conn.Close();
                return list;
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { conn.Close(); }
        }

    }
}
